// pages/addFinance/addFinance.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payType:0,
    typeIdx:'',
    budgetIdx:'',
    campusIdx:'',
    projectIdx:'',
    typeArr: ['学费', '服装', '比赛', '活动', '其他'],
    budgetArr: ['收入', '支出'],
    campusArr:[],
    projectArr:[]
  },

  
  //缴费方式
  selectPayType(e) {
    var payType = e.currentTarget.id;
    console.log(payType)
    this.setData({
      payType
    })

  },
//项目选择
  selectProject(e){
    var projectIdx = e.detail.value;
    var nowArr = this.data.projectArr[projectIdx];
    this.setData({
      projectIdx,
      project_id:nowArr.name
    })
  },

  //选择校区
  selectSchool(e) {
    var idx = e.detail.value;
    var nowArr = this.data.campusArr[idx];
    this.setData({
      campusIdx: idx,
      campus: nowArr.name
    })
  },

  //类型选择
  selectType(e){
    var typeIdx = e.detail.value;
    this.setData({
      typeIdx
    })
  },

  //收支选择
  selectBudget(e){
    var budgetIdx = e.detail.value;
    this.setData({
      budgetIdx
    })
  },

  //获取校区、项目、班级
  getSchool() {
    api.fetchApi('reception/getsearch', {
      token: wx.getStorageSync('token')
    }).then(res => {
      var classArr = res.class;
      var projectArr = res.project;
      var campusArr = res.campus;
      campusArr.push({ id: '', name: "其他" })
      //console.log(campusArr, classArr, projectArr)
      this.setData({
        // classArr,
        projectArr: campusArr,
        campusArr: projectArr
      })

    })
  },



//表单提交
  formSubmit(e){
    var formValue = e.detail.value
    console.log(formValue)
    var params={
      token: wx.getStorageSync('token'),
      campus:this.data.campus,
      project_id: this.data.project_id,
      type: formValue.type,
      pay_type: this.data.payType,
      remark: formValue.remark,
      amount: formValue.amount,
      budget: this.data.budgetIdx
    }
    if (!this.data.campus) {
      api.showToast('请填写校区', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!this.data.project_id) {
      api.showToast('请填写项目', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!formValue.type) {
      api.showToast('请填写类型', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!this.data.budgetIdx) {
      api.showToast('请填写收支', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!formValue.amount) {
      api.showToast('请填写收支金额', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    

    api.fetchApi('reception/add',params).then(res=>{
      api.showToast('添加成功').then(()=>{
        setTimeout(() => {
          wx.navigateBack({

          })
        }, 1500)
      })
      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSchool() 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})